<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./css/bootstrap.min.css">
    <style>

    </style>
</head>

<body>
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">添加图书</h3>
        </div>
        <div class="panel-body form-inline">
            <div class="input-group">
                <div class="input-group-addon">书名</div>
                <input type="text" class="form-control">
            </div>
            <div class="input-group">
                <div class="input-group-addon">作者</div>
                <input type="text" class="form-control">
            </div>
            <div class="input-group">
                <div class="input-group-addon">出版社</div>
                <input type="text" class="form-control">
            </div>
            <button class="btn btn-primary">添加</button>
        </div>
    </div>
    <!-- 表格 -->
    <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <td>id</td>
                <td>书名</td>
                <td>作者</td>
                <td>出版社</td>
                <td>操作</td>
            </tr>
        </thead>

        <tbody id="bks">

        </tbody>
    </table>
</body>

</html>
<script src="./js/jquery.js"></script>
<script>
    $(function () {
        // 查找所有图书
        function getbk() {
            $.ajax({
                type: "get",
                url: "http://www.liulongbin.top:3006/api/getbooks",
                success: (res) => {
                    var rwos = [];
                    $.each(res.data, function (i, item) {
                        rwos.push(
                            `<tr>
                        <td>${item.id}</td>
                        <td>${item.bookname}</td>
                        <td>${item.author}</td>
                        <td>${item.publisher}</td>
                        <td><a href='javascript:;' class='del' data-id='${item.id}'>删除</a></td>
                        </tr>`
                        )
                    })
                    $('#bks').empty().append(rwos.join(''))
                }
            })
        } getbk();
        // 删除图书
        $('tbody').on('click', '.del', function () {
            const ID = $(this).attr('data-id');
            $.get('http://www.liulongbin.top:3006/api/delbook', { id: ID }, function (res) {
                console.log(res.status)
                // if (res.status !== 200) {
                //     alert('删除图书失败');
                // }
                getbk();
            })
        })
        //添加图书
        $('button').on('click', () => {
            const bookname = $('input').eq(0).val().trim();
            const author = $('input').eq(1).val().trim();
            const publisher = $('input').eq(2).val().trim();
            console.log(bookname);
            if (bookname <= 0 || author <= 0 || publisher <= 0) {
                alert('信息不完整');
            } else {
                $.post('http://www.liulongbin.top:3006/api/addbook', { bookname: bookname, author: author, publisher: publisher }, (res) => {
                    if (res.status != 201) {
                        alert(res.msg)
                    }
                })
                getbk();
                $('input').eq(0).val('');
                $('input').eq(1).val('');
                $('input').eq(2).val('');
            }
        })
    })
</script>
<script src="./js/bootstrap.min.js"></script>